package com.hontech.libratest.activity.fragment.sub

import android.view.View
import android.widget.Button
import com.hontech.libratest.R
import com.hontech.libratest.activity.window.showProg
import com.hontech.libratest.conn.beepOff
import com.hontech.libratest.conn.beepOn
import com.hontech.libratest.conn.doorOpen
import com.hontech.libratest.conn.ledClose
import com.hontech.libratest.conn.ledOpen
import com.hontech.libratest.utils.onClick

class OtherSub(view: View) {

    private val btnOpenDoor = view.findViewById<Button>(R.id.id_main_btn_door_open)
    private val btnBeepOn = view.findViewById<Button>(R.id.id_main_btn_beep_on)
    private val btnBeepOff = view.findViewById<Button>(R.id.id_main_btn_beep_off)
    private val btnLedOpen = view.findViewById<Button>(R.id.id_main_btn_led_open)
    private val btnLedClose = view.findViewById<Button>(R.id.id_main_btn_led_close)

    init {
        btnOpenDoor.onClick(::onClickOpenDoor)
        btnBeepOn.onClick(::onClickBeepOn)
        btnBeepOff.onClick(::onClickBeepOff)
        btnLedOpen.onClick(::onClickLedOpen)
        btnLedClose.onClick(::onClickLedClose)
    }

    private suspend fun onClickLedOpen() {
        val w = showProg(btnBeepOff, "LED打开")
        try {
            ledOpen()
            w.success("LED打开成功")
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }

    private suspend fun onClickLedClose() {
        val w = showProg(btnBeepOff, "LED关闭")
        try {
            ledClose()
            w.success("LED关闭成功")
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }

    private suspend fun onClickOpenDoor() {
        val w = showProg(btnBeepOff, "打开大门")
        try {
            doorOpen()
            w.success("打开大门成功")
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }

    private suspend fun onClickBeepOn() {
        val w = showProg(btnBeepOff, "蜂鸣器打开")
        try {
            beepOn()
            w.success("蜂鸣器打开成功")
        } catch (e: Exception) {
            w.success(e.message!!)
            e.printStackTrace()
        }
    }

    private suspend fun onClickBeepOff() {
        val w = showProg(btnBeepOff, "蜂鸣器关闭")
        try {
            beepOff()
            w.success("蜂鸣器关闭成功")
        } catch (e: Exception) {
            w.success(e.message!!)
            e.printStackTrace()
        }
    }

}